package com.krystal.controller;

import com.krystal.bean.Result;
import com.krystal.mapper.CovidMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.StreamingHttpOutputMessage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author Krystal1
 * @date 2021/5/2 -20:23
 */
@Controller
public class SelectController {

    private String[] provinceArray={"上海", "云南", "广东","台湾","福建","香港","澳门","四川","陕西","海南","浙江","北京","江苏","天津","山西","湖北","河南",
            "山东","重庆","湖南","内蒙古","广西","甘肃","黑龙江","河北","安徽","新疆","江西","吉林","辽宁","贵州","宁夏","青海","西藏"};

    private String[][] citiesArray={
            {"境外输入","外地来沪","浦东新区","宝山区","黄浦区","闵行区","徐汇区","静安区","松江区","长宁区","普陀区","杨浦区","嘉定区","奉贤区","虹口区","青浦区","金山区","崇明区","境外来沪"},
            {"德宏州","境外输入","昆明","昭通","西双版纳","玉溪","曲靖","大理州","红河州","保山","丽江","普洱","楚雄州","文山州","临沧"},
            {"广州","深圳","佛山","肇庆","清远","珠海","东莞","中山","惠州","湛江","江门","汕头","梅州","茂名","阳江","揭阳","韶关","潮州","汕尾","河源"},
            {"台湾"},
            {"境外输入人员","福州","莆田","泉州","厦门","宁德","漳州","南平","三明","龙岩"},
            {"香港"},
            {"澳门"},
            {"成都","甘孜州","达州","南充","广安","泸州","巴中","绵阳","内江","德阳","遂宁","攀枝花","凉山州","宜宾","自贡","眉山","雅安","广元","资阳","乐山","阿坝州"},
            {"境外输入","西安","安康","汉中","咸阳","渭南","宝鸡","延安","铜川","商洛","榆林","韩城","杨凌"},
            {"海口","三亚","儋州","万宁","澄迈","昌江","琼海","临高","陵水","定安","文昌","东方","保亭","乐东","琼中"},
            {"境外输入","温州","杭州","宁波","台州","金华","嘉兴","绍兴","省十里丰监狱","丽水","衢州","湖州","舟山"},
            {"境外输入","丰台区","大兴区","海淀区","朝阳区","西城区","顺义区","昌平区","外地来京","通州区","房山区","东城区","石景山区","怀柔区","密云区","门头沟区","延庆区","待明确地区"},
            {"境外输入","南京","苏州","徐州","淮安","无锡","常州","连云港","南通","泰州","盐城","扬州","宿迁","镇江"},
            {"境外输入","宝坻区","河东区","滨海新区","河北区","北辰区","南开区","和平区","外地来津","东丽区","宁河区","河西区","西青区","武清区","红桥区","津南区"},
            {"境外输入","晋中","太原","运城","大同","晋城","长治","朔州","忻州","吕梁","阳泉","临汾"},
            {"武汉","孝感","黄冈","荆州","鄂州","随州","襄阳","黄石","宜昌","荆门","咸宁","十堰","仙桃","天门","恩施州","潜江","神农架林区"},
            {"境外输入","信阳","郑州","南阳","驻马店","商丘","周口","平顶山","新乡","安阳","许昌","漯河","焦作","洛阳","开封","鹤壁","濮阳","三门峡","济源"},
            {"境外输入","济宁","青岛","临沂","济南","烟台","潍坊","威海","聊城","德州","泰安","淄博","枣庄","菏泽","日照","滨州"},
            {"境外输入","万州区","江北区","云阳县","合川区","綦江区","长寿区","奉节县","九龙坡区","开州区","忠县","渝中区","垫江县","潼南区","渝北区","两江新区","南岸区","石柱县","大足区","巫溪县","铜梁区","丰都县","巫山县","沙坪坝区","璧山区","荣昌区","大渡口区","巴南区","涪陵区","永川区","江津区","梁平区","高新区","黔江区","城口县","彭水县","武隆区","秀山县","酉阳县","万盛经开区"},
            {"境外输入","长沙","岳阳","邵阳","常德","株洲","娄底","益阳","衡阳","永州","怀化","郴州","湘潭","湘西自治州","张家界"},
            {"境外输入人员","呼伦贝尔","鄂尔多斯","包头","赤峰","锡林郭勒盟","巴彦淖尔","呼和浩特","通辽","乌兰察布","乌海市","兴安盟"},
            {"境外输入","南宁","北海","桂林","河池","柳州","防城港","玉林","来宾","钦州","贵港","梧州","贺州","百色"},
            {"境外输入","兰州","天水","平凉","定西","甘南","白银","陇南","庆阳","临夏","张掖","金昌","省级（湖北输入）"},
            {"绥化","哈尔滨","境外输入","双鸭山","鸡西","齐齐哈尔","牡丹江","大庆","黑河","七台河","佳木斯","鹤岗","大兴安岭","待明确","伊春"},
            {"保定","石家庄","邢台","唐山","沧州","张家口","境外输入","廊坊","邯郸","秦皇岛","衡水","承德"},
            {"合肥","蚌埠","阜阳","亳州","安庆","六安","宿州","马鞍山","芜湖","铜陵","淮北","淮南","池州","滁州","黄山","宣城","境外输入"},
            {"乌鲁木齐","喀什地区","伊犁州","兵团第四师","昌吉州","兵团第八师石河子市","兵团第九师","吐鲁番","巴州","兵团第十二师","兵团第六师五家渠市","兵团第七师","阿克苏地区","待明确地区"},
            {"南昌","新余","上饶","九江","宜春","赣州","抚州","萍乡","吉安","鹰潭","境外输入","景德镇","赣江新区"},
            {"通化","长春","吉林市","四平","辽源","延边","公主岭","松原","梅河口","白城"},
            {"大连","境外输入","沈阳","锦州","葫芦岛","丹东","盘锦","阜新","铁岭","朝阳","鞍山","本溪","辽阳","营口","抚顺"},
            {"贵阳","遵义","毕节","黔南州","六盘水","铜仁","黔东南州","安顺","黔西南州","境外输入"},
            {"银川","吴忠","固原","中卫","宁东","石嘴山"},
            {"西宁","海北州"},
            {"拉萨"}
    };

    @Autowired
    private CovidMapper covidMapper;
    //加载数据

    @ResponseBody
    @RequestMapping("selectCondition")
    public Map formResponse(HttpServletRequest request){
        int confirmed=0;
        int province;
        int city;
        HashMap<String,Object> map = new HashMap<>();
        String p = request.getParameter("provinceShortName");//省份短名字下标
        String c = request.getParameter("cityName");                             //城市下标
        System.out.println(p);
        System.out.println(c);

        if(p!=null&&c!=null){
             province=Integer.parseInt(p);
             city = Integer.parseInt(c);
            if(province>=0&&city>=0){
                confirmed = covidMapper.getCovidCurrentConfirmedCount(provinceArray[province], citiesArray[province][city]);
                System.out.println("该地区确诊"+confirmed+"人");
                map.put("confirmed",confirmed);
                return map;
            }
        }
        //否则，输入错误 不执行sql 并返回-100
        map.put("confirmded",-100);         //提示用户重新选择
        System.out.println("输入有误");
        return map;
        //String msg;
//        if(confirmed<=2){
//             msg = "该地区为低风险地区，"+"现行确诊人数为"+confirmed+"人";
//            return msg;
//        }else if(confirmed<=8){
//             msg="该地区为中风险地区，"+"现行确诊人数为"+confirmed+"人,请注意防护！";
//            return msg;
//        }else {
//            msg="该地区风险较高，"+"现行确诊人数为"+confirmed+"人,出行请注意防护！";
//            return msg;
//        }
    }

    @ResponseBody
    @RequestMapping("getAreaDetail")
    public Map<String, Object> selectResponse(HttpServletRequest request){
        int province;int city;
        Map<String,Object> areaDetail = new HashMap<>();
        String p = request.getParameter("provinceShortName");//省份短名字下标
        String c = request.getParameter("cityName");    //城市下标
        System.out.println("省份下标："+p+",城市下标："+c);
        if(p!=null&&c!=null){
            province=Integer.parseInt(p);
            city = Integer.parseInt(c);
            if(province>=0&&city>=0){
                areaDetail = covidMapper.getAreaDetail(provinceArray[province], citiesArray[province][city]);
                String provinceCity;//把省份短名和城市名一起返回给前端
               if(provinceArray[province].equals("台湾")||provinceArray[province].equals("香港")||provinceArray[province].equals("澳门")){
                   provinceCity=provinceArray[province];
               }else {
                   provinceCity = provinceArray[province]+citiesArray[province][city];
               }
                areaDetail.put("provinceCity",provinceCity);
                System.out.println(areaDetail);
                return areaDetail;
            }
        }
        return areaDetail;
    }
}
